
package com.dg.acb.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

public class DigestSignUtil {
    private static char[] sHexChar = {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
    };

    public static String genSign(File file) {
        FileInputStream localInputStream = null;
        try {
            localInputStream = new FileInputStream(file);
            byte[] arrayOfByte = new byte[262144];
            MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
            int i = 0;
            while ((i = localInputStream.read(arrayOfByte)) > 0) {
                localMessageDigest.update(arrayOfByte, 0, i);
            }
            return toHexString(localMessageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (localInputStream != null) {
                try {
                    localInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private static String toHexString(byte[] data) {
        StringBuilder sb = new StringBuilder(data.length * 2);
        for (int i = 0; i < data.length; i++) {
            sb.append(sHexChar[((data[i] & 0xF0) >>> 4)]);
            sb.append(sHexChar[(data[i] & 0xF)]);
        }
        return sb.toString();
    }

    public static String genLittleFileSign(File file) {
        String value = "";
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            MappedByteBuffer byteBuffer = in.getChannel().map(
                    FileChannel.MapMode.READ_ONLY, 0,
                    file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }

    public static String genSign(Map<String, String> params) {
        if (params.size() == 0) {
            return "";
        }
        Set<String> set = params.keySet();
        String[] paramKey = set.toArray(new String[set.size()]);
        Arrays.sort(paramKey);
        StringBuilder paramStr = new StringBuilder();
        for (String param : paramKey) {
            paramStr.append(param).append("=").append(params.get(param));
        }
        return md5(paramStr.toString());
    }

    public static String md5(String data) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(data.getBytes());
            byte[] bytes = md5.digest();
            StringBuilder buf = new StringBuilder();
            for (byte bb : bytes) {
                if ((bb & 0xff) < 16) {
                    buf.append('0');
                }
                buf.append(Integer.toHexString(0xFF & bb));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
